package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	// 声明数组
	var strArr [5]string

	// 数组初始化
	var intArr [5]int
	intArr = [5]int{1, 2, 3, 4, 5}
	fmt.Println(intArr)

	var arr2 = [5]int{1, 2, 3, 4, 5} // 类型自动推断
	fmt.Println(arr2)
	strArr[0] = "a"
	strArr[1] = "b"
	strArr[2] = "c"
	strArr[3] = "d"
	strArr[4] = "e"

	// 声明可变长数组
	var floatArr = [...]float32{1.0, 2.0, 3.0, 4.0, 5.0}
	fmt.Println(floatArr)
	var arr4 = [...]int{2: 66, 0: 33, 1: 99, 3: 88} // 2的位置66，0位置33
	fmt.Println(arr4)
	for i, f := range floatArr {
		fmt.Printf("index=%+v, value=%d", i, f)
	}

	// 二维数组
	var intSecondArr [3][4]int
	intSecondArr = [3][4]int{
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11},
	}
	intThreeArr := [...][4]int{ // 4列，无限行的二维数组
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11},
		{12, 13, 14, 15},
		{16, 17, 18, 19},
	}
	fmt.Println("intSecondArr:\n", intSecondArr, "intThreeArr:\n", intThreeArr)
	// 输出二维数组
	for i, v := range intSecondArr {
		for j, v1 := range v {
			fmt.Printf("第%d行第%d列：%d \n", i+1, j+1, v1)
		}
	}

	for i := 0; i < len(intSecondArr); i++ { // len(intSecondArr)获取到的是intSecondArr的行数，intSecondArr[i] 表示一个一维数组
		for j := 0; j < len(intSecondArr[i]); j++ { // 按照一维数组的方式遍历即可
			fmt.Printf("第%d行第%d列：%d \n", i+1, j+1, intSecondArr[i][j])
		}
	}

	// 数组结构体
	var sliceArr = [...]struct {
		name string
		age  int8
	}{
		{name: "小明", age: 12},
		{name: "中明", age: 12},
		{name: "大明", age: 12}, // 别忘了最后一行的逗号。
	}

	for i, s := range sliceArr {
		fmt.Println(i, s)
	}

}

func randArr() {
	// 若想做一个真正的随机数，要种子
	// seed()种子默认是1
	//rand.Seed(1)
	rand.Seed(time.Now().Unix())

	var b [10]int
	for i := 0; i < len(b); i++ {
		// 产生一个0到1000随机数
		b[i] = rand.Intn(1000)
	}
	sum := sumArr(b)
	fmt.Printf("sum=%d\n", sum)
}

// 求元素和
func sumArr(a [10]int) int {
	var sum int = 0
	for i := 0; i < len(a); i++ {
		sum += a[i]
	}
	return sum
}
